/* ***********************
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is ads.js module.
 *
 * The Initial Developer of the Original Code is Rob Gerns.
 *
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 *
 **************************/
function getAds(objXmlDocument) {
    /***********************************************
    * Function Name: getAds
    *
    * Purpose: Check to see if any ads are defined in the
    * Oister XML.  If so, retrieve the corresponding values.
    ************************************************/
    var intCounter = 0;
    var objAds = objXmlDocument.getElementsByTagName("advertisement"); 
    var intAdsCount = objAds.length;
    var strAd = "";
    var strAdCaption = "";
    var strAdHide = "";
    var strAdImage = "";
    var strAdLink = "";
    
    if (intAdsCount > 0) {
        //Set up a loop to get all of the ads and associated attributes.
        for (intCounter =0; intCounter < intAdsCount; intCounter++) {
            strAd = objAds[intCounter].firstChild.nodeValue; 
            //Check to see if the "adlink" attribute was set.
            strAdLink = objAds[intCounter].getAttribute("adlink");
            if (strAdLink === undefined) {
                strAdLink = "";
            }
            //Check to see if the "image" attribute was set.
            strAdImage = objAds[intCounter].getAttribute("image");
            if (strAdImage === undefined) {
                strAdImage = "";
            }
            //Check to see if the "caption" attribute was set.
            strAdCaption = objAds[intCounter].getAttribute("caption");
            if (strAdCaption === undefined) {
                strAdCaption = "";
            }
            //Check to see if the "hide" attribute was set.
            strAdHide = objAds[intCounter].getAttribute("caption");
            if (strAdHide === undefined) {
                strAdHide = "";
            } 
				    //Call writeAds to generate the appropriate HTML.
		        writeAds(intCounter, strAd, strAdLink, strAdImage, strAdCaption, 
		            strAdHide);
        }
    }		
}
function writeAds(intCounter, strAd, strAdLink, strAdImage, strAdCaption, 
    strAdHide) {
    /***********************************************
    * Function Name: writeAds
    *
    * Purpose: Create the appropriate HTML to display 
    * the ads and associated attributes.
    ************************************************/
    var intCounterTwo = intCounter + 1;
    var strAdId = "advertisement" + intCounterTwo;
    var objDivAd = document.getElementById(strAdId);
 
    objDivAd.className = "advertisement";
    //If the ad "hide" attribute is set to true, then don't display the ad.
    if (strAdHide === "true") {
        objDivId.style.display = "none";
    }
    //Otherwise write the appropritate HTML to show the ad and it's attributes.
    else if (strAdHide !== "true" && strAdImage !== "" && 
        strAdCaption === "") {
        objDivAd.innerHTML = strAd + "<br/><a href=\"" + strAdLink + "\">";
        objDivAd.innerHTML += "<img src=\"" + strAdImage + "\"/></a>";
    }
    else if (strAdHide !== "true" && adImage !== "" && adCaption !== "") {
        objDivAd.innerHTML = strAd + "<br/><a href=\"" + strAdLink +"\">";
        objDivAd.innerHTML += "<img src=\"" + strAdImage + "\" alt=\"";
        objDivAd.innerHTML += strAdCaption + "\"/></a>";
    }
    else if (strAdHide !== "true" && strAdImage === "" && 
        strAdCaption === "") {
        objDivAd.innerHTML = "" + strAd + "<br/><a href=\"" + strAdLink;
        objDivAd.innerHTML += "\">" + adLink + "</a>";
    }
    else if (strAdHide !== "true" && strAdImage === "" && 
        strAdCaption !== "") {
        objDivAd.innerHTML = "" + strAd + "<br/><a href=\"" + strAdLink;
        objDivAd.innerHTML += "\"title=\"" + strAdCaption + "\">";
        objDivAd.innerHTML += strAdLink + "</a>";
    }
}